/**
 * SysDictServiceImpl.java
 * Created at 2017-1-23
 * Created by guhuajie
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.rms.system.model.SysDict;
import com.broadtext.rms.system.service.ISysDictService;

/**
 * <p>ClassName: SysDictServiceImpl</p>
 * <p>Description: 字典业务实现类</p>
 * <p>Author: guhuajie</p>
 * <p>Date: 2017-1-23</p>
 */
@Service("sysDictService")
public class SysDictServiceImpl extends EntityServiceImpl<SysDict> implements ISysDictService {

    /**
     * 
     * <p>Description: 注入字典Dao</p>
     * @param entityDao 字典Dao
     */
    @Autowired
    @Qualifier("sysDictDao")
    public void setEntityDao(EntityDao<SysDict> entityDao) {
        super.entityDao = entityDao;
    }

    /**
     * <p>Description: 更新字典</p>
     * @param dict 字典model
     * @return 更新结果
     * @throws DaoException 异常
     */
    @Override
    public String updateDict(SysDict dict) throws DaoException {
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        //验证唯一性
        queryMap.put("parentKey", dict.getParentKey());
        queryMap.put("dictKey", dict.getDictKey());
        if (this.entityDao.judgeValidate("com.broadtext.rms.system.model.SysDict.judgeUnique", queryMap)) {
            return "duplicate";
        }
        this.entityDao.insertRecord("com.broadtext.rms.system.model.SysDict.updateByPrimaryKey", dict);
        return "success";
    }

    /**
     * <p>Description: 插入字典，同时要对字典进行校验</p>
     * @param dict 字典表model 
     * @return  操作结果
     * @throws DaoException 异常
     */
    @Override
    public String insertDict(SysDict dict) throws DaoException {
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        //验证唯一性
        queryMap.put("dictName", dict.getParentKey());
        queryMap.put("dictKey", dict.getDictKey());
        if (this.entityDao.judgeValidate("com.broadtext.rms.system.model.SysDict.judgeUnique", queryMap)) {
            return "duplicate";
        }
        this.entityDao.insertRecord("com.broadtext.rms.system.model.SysDict.insertRecord", dict);
        return "success";
    }
    
    /**
     * 
     * <p>Description: 删除字典详情</p>
     * @param idArray 主键数组
     * @param updateUser 更新人
     * @throws DaoException 异常
     */
    public void deleteDictDetail(String[] idArray, String updateUser) throws DaoException {
        if(StringUtils.isBlank(updateUser)){
        	return;
        }
    	
    	for (String id : idArray) {
            //逻辑删除对应字典详情
            Map<String, Object> map;
            map = new HashMap<String, Object>();
            map.put("delFlag", "1");
            map.put("updateUser", updateUser);
            map.put("updateDate", new Date());
            map.put("pkId", id);
            this.entityDao.updateRecord("com.broadtext.rms.system.model.SysDictIndex.updateDictDetailByDictKey", map);
        }
    }

}
